<template>
  <div class="page-live mod-live-liveRoom">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="name"
            :label="$t('live.liveName')+':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.name"
              type="text"
              :placeholder="$t('live.liveName')"
            />
          </el-form-item>
          <el-form-item
            prop="nickName"
            :label="$t('live.anchorName')+':'"
            class="search-form-item"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              :placeholder="$t('live.anchorName')"
            />
          </el-form-item>
          <el-form-item
            prop="liveStatus"
            :label="$t('live.liveStatus')+':'"
            class="search-form-item"
          >
            <el-select
              v-model="searchForm.liveStatus"
              :placeholder="$t('live.liveStatus')"
            >
              <el-option
                :label="$t('live.living')"
                :value="1"
              />
              <el-option
                :label="$t('live.noStart')"
                :value="0"
              />
              <el-option
                :label="$t('live.finished')"
                :value="2"
              />
              <el-option
                :label="$t('live.stop')"
                :value="3"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('live.startTime')+':'">
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              :range-separator="$t('date.tip')"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
              :default-time="defaultTime"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch"
            >
              {{ $t('shop.resetMap') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('live:liveRoom:save')"
          class="default-btn primary-btn"
          @click="onAddOrUpdate()"
        >
          {{ $t("crud.addTitle") }}
        </div>
      </div>
      <div class="table-con live-room-table">
        <el-table
          ref="liveRoomTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            align="left"
            prop="name"
            :label="$t('live.liveName')"
            min-width="260"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="nickName"
            :label="$t('live.anchorName')"
            min-width="200"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.nickName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="userMobile"
            :label="$t('live.anchorMobile')"
            min-width="180"
          />
          <el-table-column
            align="left"
            prop="liveStatus"
            :label="$t('live.liveStatus')"
            min-width="120"
          >
            <template #default="scope">
              <span
                v-if="scope.row.liveStatus === 1"
                class="tag-text"
              >{{ $t("live.living") }}</span>
              <span
                v-if="scope.row.liveStatus === 0"
                class="tag-text"
              >{{
                $t("live.noStart")
              }}</span>
              <span
                v-if="scope.row.liveStatus === 2"
                class="tag-text"
              >{{
                $t("live.finished")
              }}</span>
              <span
                v-if="scope.row.liveStatus === 3"
                class="tag-text"
              >{{
                $t("live.stop")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="startTime"
            :label="$t('live.liveStartTime')"
            min-width="160"
          />
          <el-table-column
            align="left"
            prop="endTime"
            :label="$t('live.liveEndTime')"
            min-width="160"
          />
          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            min-width="230"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('live:liveRoom:update') && (scope.row.liveStatus === 0 || scope.row.liveStatus === 3)"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t("live.edit") }}
                </div>
                <div
                  v-if="scope.row.liveStatus !== 0 && scope.row.liveStatus !== 3"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t("live.view") }}
                </div>
                <div
                  v-if="isAuth('live:liveRoom:delete') && scope.row.liveStatus !== 1"
                  class="default-btn text-btn"
                  type="text"
                  @click="onDelete(scope.row.roomId)"
                >
                  {{ $t('resource.Delete') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox, ElMessage } from 'element-plus'
import { isAuth } from '@/utils/index'

let tempSearchForm = null // 保存上次点击查询的请求条件

const dataList = ref([])
const dateRange = ref([])
const defaultTime = ref([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
])
// 修改
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})

const searchForm = reactive({
  name: '',
  nickName: '',
  liveStatus: '',
  roomTop: ''
})

onMounted(() => {
  getDataList()
})

const getDataList = (newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/multishop/live/liveRoom/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: page.currentPage,
          size: page.pageSize,
          startTime: dateRange.value === null ? null : dateRange.value[0], // 开始时间
          endTime: dateRange.value === null ? null : dateRange.value[1] // 结束时间
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
  })
}
// 新增 / 修改
const router = useRouter()
const onAddOrUpdate = (data) => {
  router.push({
    path: '/marketing/live/add-or-update'
  })
  sessionStorage.setItem('b2cLiveRoomData', JSON.stringify(data))
}

const onDelete = (id) => {
  ElMessageBox.confirm($t('admin.isDeleOper'), $t('resource.tips'), {
    confirmButtonText: $t('crud.filter.submitBtn'),
    cancelButtonText: $t('crud.filter.cancelBtn'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/multishop/live/liveRoom/' + id),
      method: 'delete',
      data: http.adornData({})
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  })
}

// 条件查询
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

// 重置表单
const searchFormRef = ref(null)
const onResetSearch = () => {
  searchFormRef.value.resetFields()
  dateRange.value = []
}
// 刷新回调
const refreshChange = () => {
  getDataList(page)
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}

const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}
</script>
